package com.hl.card.credit.service.impl;

import com.hl.card.credit.domain.PublicOrders;
import com.hl.card.credit.mapper.PublicOrdersMapper;
import com.hl.card.credit.pub.common.RedisKey;
import com.hl.card.credit.pub.entity.*;
import com.hl.card.credit.pub.model.PublicOrdersAllModel;
import com.hl.card.credit.pub.utils.CommonUtil;
import com.hl.card.credit.pub.utils.FormValid;
import com.hl.card.credit.pub.utils.UuidCodeUtils;
import com.hl.card.credit.service.PublicOrdersService;
import com.hl.card.credit.utill.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class PublicOrdersServiceImpl implements PublicOrdersService {

    @Autowired
    private PublicOrdersMapper dao;

    @Autowired
    private RedisUtil redisUtil;


    @Transactional(propagation= Propagation.REQUIRED,isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int addPublicOrders(PublicOrdersAllModel model) {
        Map<String, Object> map=new HashMap<String, Object>();
        if(CommonUtil.isEmpty(model.getOrder_code())){
            return 2;
        }
        //公共订单数据
        PublicOrders publicOrders=PublicOrders.Init(map, model);
        dao.insert(publicOrders);
        //订单日志
        if(CommonUtil.isNotEmpty(model.getPublicOrderLog())){
            PublicOrderLog publicOrderLog=model.getPublicOrderLog();
//            publicOrderLogService.add(publicOrderLog);
        }
        //订单时间节点 public_order_time_node
        if(CommonUtil.isNotEmpty(model.getPublicOrderTimeNode())){
            PublicOrderTimeNode publicOrderTimeNode=PublicOrderTimeNode.init(map, model);
//            publicOrderTimeNodeService.add(publicOrderTimeNode);
        }
        return 1;
    }


    @Override
    public String addOrderCode(PublicOrdersAllModel param) {
        FormValid.validNull(param.getBusines_species(), "业务种类为空");
        FormValid.validNull(param.getPolicy_name(), "商户三字码为空");
        FormValid.validNull(param.getBusiness_type(), "业务类型为空");

        String str=param.getBusines_species()+param.getPolicy_name()+param.getBusiness_type();
        int num=redisUtil.incrExpireOrderCode(RedisKey.ORDER_COUNT+param.getBusines_species()+":"+param.getPolicy_name()+":"+param.getBusiness_type(),1,1);
        Map<String, String> map= UuidCodeUtils.getOrderCode(str,num);
        System.out.println("addOrderCode(PublicOrdersAllModel param)param:"+param);
        System.out.println("addOrderCode(PublicOrdersAllModel param)map:"+map);
        System.out.println();
        return map.get("code");
    }



}
